//
//  LYChangeHeaderImageView.m
//  SZBank
//
//  Created by LuoYao on 16/7/18.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "LYChangeHeaderImageView.h"
#import "LYHeaderImageEditorView.h"

@interface LYChangeHeaderImageView ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIButton *_senderBtn;/**< 传来的btn */
}
@property (nonatomic) UIImagePickerController *imagePickerController;/**< 图片拾取器 */

@end
@implementation LYChangeHeaderImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}





- (void)alertChoose:(UIButton *)sender{
    
    _senderBtn = sender;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"上传头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *photo = [UIAlertAction actionWithTitle:@"系统相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self systemPhotoLibary];
    }];
    
    
    UIAlertAction *cama = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    
    [alertController addAction:photo];
    [alertController addAction:cama];
    
    //ipad独有，需要传入位置
    alertController.popoverPresentationController.sourceView = sender;
    alertController.popoverPresentationController.sourceRect = sender.bounds;
    
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}


- (void)systemPhotoLibary{
    //媒体支持格式
    NSArray *mediaType = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    //是否支持当前格式
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        NSLog(@"设备不支持相册");
        return;
    }
    
    self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//sourceType
    self.imagePickerController.mediaTypes = @[mediaType.firstObject];//媒体格式
    self.imagePickerController.allowsEditing = NO;//允许编辑
    
    self.imagePickerController.popoverPresentationController.sourceRect = _senderBtn.bounds;
    self.imagePickerController.popoverPresentationController.sourceView = _senderBtn;
    
    
    
    //推送
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self.imagePickerController animated:YES completion:nil];
}


#pragma mark - UIImagePickerDelegate协议

//用户点击取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//用户选择了媒体
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    
    NSLog(@"%@",info);

    //获取图片
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];

    
    
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    //跳转到自定义页面
    LYHeaderImageEditorView *imageEditorView = [[LYHeaderImageEditorView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT) withImage:image];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:imageEditorView];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{
    
}


#pragma mark - getter
- (UIImagePickerController *)imagePickerController{

    if (!_imagePickerController) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.delegate = self;
        
        //选择器样式
        _imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
      

    }
    return _imagePickerController;
}



@end
